package com.example.hotelmanager.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.hotelmanager.entity.*;
import com.example.hotelmanager.entity.vo.Response;
import com.example.hotelmanager.entity.vo.ResponseMsg;
import com.example.hotelmanager.service.*;
import com.example.hotelmanager.utils.JwtUtill;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.security.auth.callback.LanguageCallback;
import javax.servlet.http.HttpServletRequest;
import java.awt.*;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;
import java.util.Map;

/**
 * @author :chenjie
 * @date :Created 2022/11/30 12:25
 */
@CrossOrigin
@RestController
@Slf4j
public class CheckInController {
    @Autowired
    private JwtUtill jwtUtill;
    @Autowired
    private CheckInService checkInService;
    @Autowired
    private FrontService frontService;
    @Autowired
    private BookMsgService bookMsgService;
    @Autowired
    private CostTypeService costTypeService;
    @Autowired
    private CostService costService;
    @Autowired
    private GuestService guestService;
    @Autowired
    private RoomService roomService;

    /**
     * 预约的入住
     * @param request
     * @param bookMsgId
     * @return
     */
    @PostMapping("/bookCheckIn")
    public Response bookCheckIn(HttpServletRequest request, int bookMsgId) {
        String num = (String) request.getAttribute("num");
        if (frontService.getById(num) != null) {

            Map<String, Object> map = checkInService.bookCheckIn(bookMsgId);
            if(map.size()==0){
                return new Response(ResponseMsg.ILLEGAL_OPERATION);//重复入住
            }
            checkInService.saveOrUpdate((CheckIn) map.get("checkIn"));
            bookMsgService.saveOrUpdate((BookMsg) map.get("bookMsg"));
                String token = jwtUtill.updateJwt(num);
                return (new Response()).success(token);

        }
        return new Response(ResponseMsg.ILLEGAL_OPERATION);//非法操作
    }
    /**
     * 没预约的用户入住
     *
     * @param request
     * @return
     */
    @PostMapping("/checkIn")
    public Response checkIn(HttpServletRequest request, Long fromTime, Long toTime, String contact, String idCard, String name, String roomId) {
        String num = (String) request.getAttribute("num");
        if (frontService.getById(num) != null) {
            CheckIn checkIn = new CheckIn(0, idCard, roomId, new Timestamp(fromTime), new Timestamp(toTime), 1);
            Guest guest = new Guest(idCard, name, contact);
            guestService.saveOrUpdate(guest);
            Room room = roomService.getById(roomId);
            int maxNum = room.getMaxNum();//多人房时
            LambdaQueryWrapper<CheckIn> queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.eq(CheckIn::getRoomId,checkIn.getRoomId());

            int num1 = checkInService.count(queryWrapper);
            if (maxNum > num1) {//根据将房间的花费情况添加到花费表
                if (num1 == 0) {
                    String costName1 = room.getRoomId() + "房间定金";
                    String costName2 = room.getRoomId() + "房间租金";
                    LambdaQueryWrapper<CostType>queryWrapper1=new LambdaQueryWrapper<>();
                    LambdaQueryWrapper<CostType>queryWrapper2=new LambdaQueryWrapper<>();
                    queryWrapper1.eq(CostType::getName,costName1);
                    queryWrapper2.eq(CostType::getName,costName2);
                    List<CostType> costTypes1 = costTypeService.list(queryWrapper1);
                    List<CostType> costTypes2 = costTypeService.list(queryWrapper2);
                    if (costTypes1 != null && costTypes2 != null && costTypes1.size() > 0 && costTypes2.size() > 0) {
                        CostType costType = costTypes1.get(0);
                        Cost cost1 = new Cost(0, costType.getId(), roomId, 1, 0);
                        costService.saveOrUpdate(cost1);

                        int costNum = (int) (toTime - fromTime) / 1000 / 60 / 60 / 24;
                        costType = costTypes2.get(0);
                        cost1 = new Cost(0, costType.getId(), roomId, costNum, 1);
                        costService.saveOrUpdate(cost1);
                    } else {
                        return new Response(ResponseMsg.FAIL);
                    }
                }
                checkInService.saveOrUpdate(checkIn);
                String token = jwtUtill.updateJwt(num);
                return (new Response()).success(token);
            }
            return new Response(ResponseMsg.FAIL);
        }
        return new Response(ResponseMsg.ILLEGAL_OPERATION);
    }

    /**
     * 退房
     * @param request
     * @return
     */
    @PostMapping("/checkOut")
    public Response checkOut(HttpServletRequest request, String roomId) {
        String num = (String) request.getAttribute("num");
        if (frontService.getById(num) != null) {
            LambdaQueryWrapper<Cost> queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.eq(Cost::getRoomId,roomId);
            queryWrapper.eq(Cost::getState,0);
            int costNum = costService.count(queryWrapper);
//            costService.remove(queryWrapper);//删除消费记录
            //查找为结算的消费数量
            if (costNum == 0) {
                costService.settleCostByRoomId(roomId);//将消费状态修改为全部结清（退房前要结清所以款项）
                LambdaQueryWrapper<CheckIn> queryWrapper1=new LambdaQueryWrapper<>();
                queryWrapper1.eq(CheckIn::getRoomId,roomId);
                checkInService.remove(queryWrapper1);
                String token = jwtUtill.updateJwt(num);
                return (new Response()).success(token);

            }
            return new Response(ResponseMsg.FAIL);//请求失效
        }
        return new Response(ResponseMsg.ILLEGAL_OPERATION);
    }
}
